﻿// -------------------------------------------------------------------------
//    @FileName         :    NFCityDynamicModule.cpp
//    @Author           :    Gao.Yi
//    @Date             :    2025-09-16
//    @Email			:    445267987@qq.com
//    @Module           :    NFCityDynamicModule
//    @Desc             :
//
// -------------------------------------------------------------------------

#include "NFCityDynamicModule.h"

#include "NFCityPacketSendUtil.h"
#include "NFCitySessionUtil.h"
#include "proto_cs.pb.h"
#include "proto_cs_cmd.nanopb.h"
#include "NFLogicCommon/NFScopedMessageHead.h"
#include "NFLogicCommon/NFWatchTime.h"
#include "Session/NFCitySession.h"

NFCityDynamicModule::NFCityDynamicModule(NFIPluginManager* p):NFMMODynamicModule(p)
{

}

NFCityDynamicModule::~NFCityDynamicModule()
{
}

int NFCityDynamicModule::OnHandleClientMessage(uint64_t unLinkId, NFDataPackage& packet)
{
    uint64_t sessionId = packet.nParam1;
    NFCitySession *pSession = NFCitySessionUtil::GetSessionObj(sessionId);
    CHECK_EXPR(pSession, -1, "NFCitySessionUtil::GetSessionObj Failed ID:{}", sessionId);

    NFScopedMessageHead scopedMessageHead(pSession->GetGlobalId(), packet.nMsgId, packet.nMsgSeq);

    NFAutoLogContext autoLogContext(pSession->GetOpenID(), pSession->GetRoleId(), packet.mModuleId, packet.nMsgId, packet.nMsgSeq);

    CHECK_EXPR(proto_ff::Proto_CS_CMD_IsValid(packet.nMsgId), -1, "invalid msg id:{}", packet.nMsgId);
    LOGSVR_TRACE_IF(packet.nMsgId != E_CS_HEARTBEAT_REQ, "recv msg:" << proto_ff::Proto_CS_CMD_Name(static_cast<proto_ff::Proto_CS_CMD>(packet.nMsgId)))

    if (m_pObjPluginManager->IsServerStopping())
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "City Server is Stopping, drop client msg:{}", packet.nMsgId);
        return -1;
    }

    int iRetCode = pSession->ActiveAndCheck(packet.nMsgId, 0, NF_ADJUST_TIMENOW(), 0);
    CHECK_PLAYER_RET(pSession->GetRoleId(), iRetCode, "Session:{} of Role:{}  Recv msg Check Invalid!MsgID:{}", pSession->GetGlobalId(), pSession->GetRoleId(), packet.nMsgId);

#ifdef NF_DEBUG_MODE
    NFWatchTime oWatchTime(packet.nMsgId, 0, packet.nMsgLen, ENST_NET_IN, &iRetCode);
#endif

    proto_ff::Proto_CSReq req;
    CLIENT_MSG_PROCESS_WITH_PRINTF(packet, req);

    uint64_t ullTimeNow = NFGetTime();
    if (ullTimeNow > (uint64_t) req.timestamp() + 100000)
    {
        LOGSVR_ERROR("recv packet delay:" << (ullTimeNow - req.timestamp()));
    }

    iRetCode = pSession->CheckPkgRate(packet.nMsgId);
    if (iRetCode)
    {
        NFCityPacketSendUtil::SendErrToClient(pSession, packet.nMsgId, (proto_ff::Proto_CS_ErrorCode) iRetCode);
        return 0;
    }

    iRetCode = OnHandleClientMessage(packet, pSession, req);
    if (iRetCode > 0)
    {
        NFCityPacketSendUtil::SendErrToClient(pSession, packet.nMsgId,  (proto_ff::Proto_CS_ErrorCode)iRetCode);
    }
    return 0;
}

int NFCityDynamicModule::OnHandleClientMessage(NFDataPackage& packet, NFCitySession* pSession, const proto_ff::Proto_CSReq& req)
{
    NFLogError(NF_LOG_DEFAULT, 0, "nMsgId:{} not handle", packet.nMsgId);
    return -1;
}
